(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.sitetpl.deleteRejectController', deleteRejectController);
	deleteRejectController.$inject = [ '$scope', '$timeout', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'sitetplService' ];
	function deleteRejectController($scope, $timeout, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, sitetplService) {
		var ctrl = this, sitetplInfo = $scope.$stateParams.params;
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.sitetpl = {};
		ctrl.sitetplScopeTmp = {groups : []};

		ctrl.init = function() {
			var params = {
					taskId : sitetplInfo.taskId
			};
			// init acct
			sitetplService.initSitetplRole(layoutConfig, $scope, ctrl);
			// init int
			sitetplService.initSiteScopeGroup(layoutConfig, $scope, ctrl);
			sitetplService.initCompanyScopeGroup(layoutConfig, $scope, ctrl);
			//
			// init general
			sitetplService.initGeneral($scope, ctrl).then(function() {
				authService.deleteReject(params).then(function(response) {
					var initSitetpl = response.data.model, initSitetplScopeTmp = {groups : []};
					// init ctrl.sitetplScopeTmp
					angular.forEach(initSitetpl.companyScopeGroups, function(companyScopeGroup, key) {
						if (!initSitetplScopeTmp.groups['companyScopeGroups' + companyScopeGroup.groupScope]) {								
							initSitetplScopeTmp.groups['companyScopeGroups' + companyScopeGroup.groupScope] = [];
						}
						initSitetplScopeTmp.groups['companyScopeGroups' + companyScopeGroup.groupScope].push(companyScopeGroup);
					});
					ctrl.layoutConfig = layoutConfig;
					$timeout(function() {
						ctrl.sitetpl = initSitetpl;
						ctrl.sitetpl.taskId = response.data.task.taskId || sitetplInfo.taskId;
						ctrl.sitetplScopeTmp = initSitetplScopeTmp;
					});
				});
			});
		};
		
		ctrl.deleteRejectSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.sitetpl.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectSubmit(ctrl.sitetpl).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		ctrl.deleteRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.sitetpl.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectCancel(ctrl.sitetpl).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sitetpl.list',
					'state' : 'cda.sitetpl.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sitetpl.list',
					'state' : 'cda.sitetpl.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);